package com.daniel.jedis;

import com.daniel.jedis.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
public class SpringBootJedis {
    
    @Autowired
    private RedisTemplate<String , Object> redisTemplate;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    // json工具
    private static final ObjectMapper mapper = new ObjectMapper();
    @Test
    public void testSpringbootJedis() throws JsonProcessingException {
        User user = new User();
        user.setName("刘亦菲");
        user.setAge("18");
        // 手动序列化
        String json = mapper.writeValueAsString(user);
        stringRedisTemplate.opsForValue().set("user:2",json);
        String val = stringRedisTemplate.opsForValue().get("user:2");
        // 手动反序列化
        User user1 = mapper.readValue(val, User.class);
        System.out.println("boot:"+user1);
    }
}
